/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import scenic.filter.Filter;
import scenic.path.Path;

/**
 * FilledPath class fills the area inside the given path.
 */
public class FilledPath extends SceneShape {
	private Path path;
	private int fillRule;
	private Tesselator tesselator;
	
	/**
	 * Constructs a filled path using the given path.
	 * 
	 * @param path the path to be filled.
	 */
	public FilledPath(Path path) {
		this.path = path;
	}
	
	void draw(DrawContext context, AffineTransform transform, ScenicColor color) {
		if(tesselator == null)
			tesselator = new Tesselator(path);

		context.renderer.setTransform(context.context, transform);
		context.renderer.color(context.context, color);
		context.renderer.setPolygonAntialiasingFilter(context.context, context.AAFilter);
		tesselator.draw(context, transform);
	}
	
	/**
	 * Sets the path to be filled.
	 * 
	 * @param path the path to be filled.
	 */
	public void setPath(Path path) {
		this.path = path;
		changed();
		if(tesselator != null)
			tesselator.setPath(path);
	}

	/**
	 * Gets the path to be filled.
	 * 
	 * @return the path to be filled.
	 */
	public Path getPath() {
		return path;
	}
	
	/**
	 * Sets the fill rule used when filling the path. The fill
	 * rule must be one of the constants defined in the FillRule
	 * interface.
	 * 
	 * @param rule the fill rule. 
	 */
	public void setFillRule(int rule) {
		fillRule = rule;
		changed();
	}

	/**
	 * Gets the fill rule.
	 */
	public int getFillRule() {
		return fillRule;
	}
	
	int getDrawType(DrawContext context, AffineTransform transform, ScenicColor color) {
		return (context.AAFilter != null) ? DRAW_SURFACE4X : DRAW_SIMPLE;
	}
	
	int getDrawType(DrawContext context, AffineTransform transform) {
		return (context.AAFilter != null && getColor().isWhite()) ? DRAW_SURFACE4X : DRAW_SIMPLE;
	}
	
	Rectangle getBounds(DrawContext context, AffineTransform transform) {
		Rectangle r = path.getBounds(transform);
		Filter filter = context.AAFilter;
		
		// Compensate for the antialiasing filter
		if(filter != null)
			r = addMargin(r, (int)Math.ceil(filter.getWidth()), (int)Math.ceil(filter.getHeight()));
		return r;
	}	
	
	protected void hide() {
		super.hide();
		if(tesselator != null)
			tesselator.free();
	}
}
